package com.niit.cms.controller;

import com.niit.cms.entity.Cart;
import com.niit.cms.entity.Userinfo;
import com.niit.cms.service.CartService;
import com.niit.cms.service.CourseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description:
 * @Author: cao_jiaqi
 * @CreateTime： 2022/7/3 15:51
 */
@Controller
public class CartController {
    @Resource
    private CartService cartService;
    @Resource
    private CourseService courseService;
    /**
     * @Description: 购物车页面
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/3 17:04
     * @param: model
     * @param: session
     * @return: java.lang.String
     **/
    @GetMapping("/cart")
    public String cart(Model model, HttpSession session){
        Userinfo userinfo = (Userinfo) session.getAttribute("user");
        List<Cart> cartList = cartService.queryListByUid(userinfo.getId());
        Integer count = 0;
        for(Cart cart : cartList){
            cart.setCourse(courseService.queryById(cart.getCourseId()));
            count++;
        }
        model.addAttribute("cartList", cartList);
        model.addAttribute("count", count);
        return "cart";
    }
}
